<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>影院管理</el-breadcrumb-item>
      <el-breadcrumb-item>影院列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="query"
            clearable
            @clear="getCinema"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getCinemaName"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加影院</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="cinemalist" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="影院名称"
          prop="name"
          align="center"
        ></el-table-column>
        <el-table-column label="创建时间" prop="time" align="center">
          <template slot-scope="scope">
            <p>{{ scope.row.time | dateFormat }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加影院 -->
    <el-dialog
      title="添加影院"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="影院名称" prop="name" label-width="100px">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="time" label-width="100px">
          <el-date-picker
            v-model="addForm.time"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCinema">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改影院信息 -->
    <el-dialog
      title="修改影院信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <!-- 内容主体区域 -->
      <el-form
        :model="editForm"
        :rules="editFormRef"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="影院名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="time" label-width="100px">
          <el-date-picker
            v-model="editForm.time"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateCinema">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      query: "",
      cinemalist: [],
      //添加表单的验证规则对象
      addFormRules: {
        name: [{ required: true, message: "请输入影院名字", trigger: "blur" }],
        time: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
      },
      //控制添加窗口显示与隐藏
      addDialogVisible: false,
      //添加用户表单数据
      addForm: {
        name: "",
        title: "",
      },
      //控制修改窗口显示与隐藏
      editDialogVisible:false,
      editForm:{},
      editFormRef: {
        name: [{ required: true, message: "请输入影院名字", trigger: "blur" }],
        time: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getCinema();
  },
  methods: {
    async getCinema() {
      const { data: res } = await this.$http.get("cinemaAll");
      if (res.status !== 200) return this.$message.error("获取失败");
      this.$message.success("获取成功");
      this.cinemalist = res.data;
    },
    //搜索影院
    async getCinemaName() {
      const { data: res } = await this.$http.get(
        "cinemaByName?name=" + this.query
      );
      if (res.status !== 200) return this.$message.error("获取失败");
      console.log(res);
      this.cinemalist = res.data;
    },
    //关闭添加影院窗口
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //删除影院
    async removeById(id) {
      //弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该影院, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      const { data: res } = await this.$http.delete("cinema/" + id);
      if (res.status !== 200) {
        return this.$message.error("删除影院失败！");
      }

      this.$message.success("删除影院成功！");
      this.getCinema();
    },
    //添加电影
    addCinema() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error("请填写必要的表单项！");
        }
        const { data: res } = await this.$http.post("cinema", {
          cinemaInfo: this.addForm,
        });
        console.log(res);
        if (res.status !== 200) {
          return this.$message.error("添加影院失败！");
        }

        this.$message.success("添加影院成功！");
        this.addDialogVisible = false;
        this.getCinema();
      });
    },
    editDialogClosed() {
      this.editDialogVisible = false;
    },
    //展示编辑用户的对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get("cinema/" + id);

      if (res.status != 200) {
        this.$message.error("获取影院失败！");
      }
      console.log(res);
      this.editForm = res.data;

      this.editDialogVisible = true;
    },
    //修改影院信息
    updateCinema(){
        this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        //发起修改用户信息的数据请求
        const { data: res } = await this.$http.put(
          'cinema',
          {id: this.editForm.id,name: this.editForm.name,time:this.editForm.time }
        )
        console.log(res);

        if (res.status !== 200) {
          return this.$message.error('更新影院信息失败！')
        }

        //关闭对话框
        this.editDialogVisible = false
        //刷新列表
        this.getCinema()
        //提示用户修改成功
        this.$message.success('更新影院信息成功！')
      })
    }
  },
};
</script>

<style>
</style>